# "Partisanship, political awareness, and retrospective evaluations, 1956-2016"
# Phil Jones
# 03.05.19

# Replication file to run individual-level regression models shown in Tables 1,2, and A2-A12, and simulated first differences shown in Figures 1 and 2. 

library(car)
library(lme4)
library(Zelig)
library(ZeligChoice)
library(ggplot2)
library(texreg)
library(broom)

load("pejones_PB_indiv.RData")

library(survey)
wdata <- svydesign(~0, weights=data$weight, data=data)

c.names <- c("Age", "White", "Income", "Education", "Female", "Independent", "Out-partisan", "Threshold 1", "Threshold 2")
c.names.aware <- c("Age", "White", "Income", "Education", "Female", "Awareness", "Independent", "Out-partisan", "Awareness \times Independent", "Awareness \times Out-partisan", "Threshold 1", "Threshold 2")


tidy.model.texreg <- function(mod){
		coefs <- coef(summary(mod))
		pvs <- pnorm(abs(coefs[,"t value"]), lower.tail=F)*2
		createTexreg(coef.names=c.names, coef=coef(mod), se=coef(summary(mod))[,2], pvalues=pvs, gof=mod$n, gof.names=c("N"), gof.decimal=F)	
	}
tidy.model.texreg.aware <- function(mod){
		coefs <- coef(summary(mod))
		pvs <- pnorm(abs(coefs[,"t value"]), lower.tail=F)*2
		createTexreg(coef.names=c.names.aware, coef=coef(mod), se=coef(summary(mod))[,2], pvalues=pvs, gof=mod$n, gof.names=c("N"), gof.decimal=F)	
	}	





########################################################################
#  DV: retro_finstat_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_finstat_f)))
years <- unique(md$year)

c.retro_finstat <- list()
x.retro_finstat <- list()
f.retro_finstat <- matrix(NA, ncol=3, nrow=length(years))

c.retro_finstat.aware <- list()
x.retro_finstat.aware <- list()
f.retro_finstat.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_finstat.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_finstat_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_finstat[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_finstat_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_finstat[[i]] <- w1
	x.retro_finstat[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_finstat_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_finstat.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_finstat.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_finstat_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_finstat.aware[[i]] <- w1
	x.retro_finstat.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_finstat <- as.data.frame(f.retro_finstat)
names(f.retro_finstat) <- c("fd", "lo", "up")
f.retro_finstat$year <- years
f.retro_finstat$dv <- "retro_finstat"
f.retro_finstat$type <- "Pocketbook"

f.retro_finstat.aware <- as.data.frame(rbind(f.retro_finstat.lo, f.retro_finstat.hi))
names(f.retro_finstat.aware) <- c("fd", "lo", "up")
f.retro_finstat.aware$year <- years
f.retro_finstat.aware$dv <- "retro_finstat"
f.retro_finstat.aware$type <- "Pocketbook"
f.retro_finstat.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))




########################################################################
#  DV: retro_cost_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_cost_f)))
years <- unique(md$year)

c.retro_cost <- list()
x.retro_cost <- list()
f.retro_cost <- matrix(NA, ncol=3, nrow=length(years))

c.retro_cost.aware <- list()
x.retro_cost.aware <- list()
f.retro_cost.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_cost.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_cost_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_cost[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_cost_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_cost[[i]] <- w1
	x.retro_cost[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_cost_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_cost.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_cost.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_cost_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_cost.aware[[i]] <- w1
	x.retro_cost.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_cost <- as.data.frame(f.retro_cost)
names(f.retro_cost) <- c("fd", "lo", "up")
f.retro_cost$year <- years
f.retro_cost$dv <- "retro_cost"
f.retro_cost$type <- "Pocketbook"

f.retro_cost.aware <- as.data.frame(rbind(f.retro_cost.lo, f.retro_cost.hi))
names(f.retro_cost.aware) <- c("fd", "lo", "up")
f.retro_cost.aware$year <- years
f.retro_cost.aware$dv <- "retro_cost"
f.retro_cost.aware$type <- "Pocketbook"
f.retro_cost.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))




########################################################################
#  DV: retro_income_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_income_f)))
years <- unique(md$year)

c.retro_income <- list()
x.retro_income <- list()
f.retro_income <- matrix(NA, ncol=3, nrow=length(years))

c.retro_income.aware <- list()
x.retro_income.aware <- list()
f.retro_income.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_income.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_income_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_income[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_income_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_income[[i]] <- w1
	x.retro_income[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_income_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_income.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_income.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_income_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_income.aware[[i]] <- w1
	x.retro_income.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_income <- as.data.frame(f.retro_income)
names(f.retro_income) <- c("fd", "lo", "up")
f.retro_income$year <- years
f.retro_income$dv <- "retro_income"
f.retro_income$type <- "Pocketbook"

f.retro_income.aware <- as.data.frame(rbind(f.retro_income.lo, f.retro_income.hi))
names(f.retro_income.aware) <- c("fd", "lo", "up")
f.retro_income.aware$year <- years
f.retro_income.aware$dv <- "retro_income"
f.retro_income.aware$type <- "Pocketbook"
f.retro_income.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))




########################################################################
#  DV: retro_econ_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_econ_f)))
years <- unique(md$year)

c.retro_econ <- list()
x.retro_econ <- list()
f.retro_econ <- matrix(NA, ncol=3, nrow=length(years))

c.retro_econ.aware <- list()
x.retro_econ.aware <- list()
f.retro_econ.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_econ.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_econ_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_econ[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_econ_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_econ[[i]] <- w1
	x.retro_econ[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_econ_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_econ.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_econ.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_econ_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_econ.aware[[i]] <- w1
	x.retro_econ.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_econ <- as.data.frame(f.retro_econ)
names(f.retro_econ) <- c("fd", "lo", "up")
f.retro_econ$year <- years
f.retro_econ$dv <- "retro_econ"
f.retro_econ$type <- "Sociotropic"

f.retro_econ.aware <- as.data.frame(rbind(f.retro_econ.lo, f.retro_econ.hi))
names(f.retro_econ.aware) <- c("fd", "lo", "up")
f.retro_econ.aware$year <- years
f.retro_econ.aware$dv <- "retro_econ"
f.retro_econ.aware$type <- "Sociotropic"
f.retro_econ.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))



########################################################################
#  DV: retro_unemp_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_unemp_f)))
years <- unique(md$year)

c.retro_unemp <- list()
x.retro_unemp <- list()
f.retro_unemp <- matrix(NA, ncol=3, nrow=length(years))

c.retro_unemp.aware <- list()
x.retro_unemp.aware <- list()
f.retro_unemp.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_unemp.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_unemp_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_unemp[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_unemp_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_unemp[[i]] <- w1
	x.retro_unemp[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_unemp_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_unemp.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_unemp.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_unemp_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_unemp.aware[[i]] <- w1
	x.retro_unemp.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_unemp <- as.data.frame(f.retro_unemp)
names(f.retro_unemp) <- c("fd", "lo", "up")
f.retro_unemp$year <- years
f.retro_unemp$dv <- "retro_unemp"
f.retro_unemp$type <- "Sociotropic"

f.retro_unemp.aware <- as.data.frame(rbind(f.retro_unemp.lo, f.retro_unemp.hi))
names(f.retro_unemp.aware) <- c("fd", "lo", "up")
f.retro_unemp.aware$year <- years
f.retro_unemp.aware$dv <- "retro_unemp"
f.retro_unemp.aware$type <- "Sociotropic"
f.retro_unemp.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))




########################################################################
#  DV: retro_inflation_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_inflation_f)))
years <- unique(md$year)

c.retro_inflation <- list()
x.retro_inflation <- list()
f.retro_inflation <- matrix(NA, ncol=3, nrow=length(years))

c.retro_inflation.aware <- list()
x.retro_inflation.aware <- list()
f.retro_inflation.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_inflation.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_inflation_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_inflation[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_inflation_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_inflation[[i]] <- w1
	x.retro_inflation[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_inflation_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_inflation.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_inflation.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_inflation_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_inflation.aware[[i]] <- w1
	x.retro_inflation.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_inflation <- as.data.frame(f.retro_inflation)
names(f.retro_inflation) <- c("fd", "lo", "up")
f.retro_inflation$year <- years
f.retro_inflation$dv <- "retro_inflation"
f.retro_inflation$type <- "Sociotropic"

f.retro_inflation.aware <- as.data.frame(rbind(f.retro_inflation.lo, f.retro_inflation.hi))
names(f.retro_inflation.aware) <- c("fd", "lo", "up")
f.retro_inflation.aware$year <- years
f.retro_inflation.aware$dv <- "retro_inflation"
f.retro_inflation.aware$type <- "Sociotropic"
f.retro_inflation.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))






########################################################################
#  DV: retro_general_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_general_f)))
years <- unique(md$year)

c.retro_general <- list()
x.retro_general <- list()
f.retro_general <- matrix(NA, ncol=3, nrow=length(years))

c.retro_general.aware <- list()
x.retro_general.aware <- list()
f.retro_general.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_general.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_general_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_general[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_general_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_general[[i]] <- w1
	x.retro_general[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_general_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_general.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_general.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_general_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_general.aware[[i]] <- w1
	x.retro_general.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_general <- as.data.frame(f.retro_general)
names(f.retro_general) <- c("fd", "lo", "up")
f.retro_general$year <- years
f.retro_general$dv <- "retro_general"
f.retro_general$type <- "NA"

f.retro_general.aware <- as.data.frame(rbind(f.retro_general.lo, f.retro_general.hi))
names(f.retro_general.aware) <- c("fd", "lo", "up")
f.retro_general.aware$year <- years
f.retro_general.aware$dv <- "retro_general"
f.retro_general.aware$type <- "NA"
f.retro_general.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))



########################################################################
#  DV: retro_business_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_business_f)))
years <- unique(md$year)

c.retro_business <- list()
x.retro_business <- list()
f.retro_business <- matrix(NA, ncol=3, nrow=length(years))

c.retro_business.aware <- list()
x.retro_business.aware <- list()
f.retro_business.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_business.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_business_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_business[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_business_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_business[[i]] <- w1
	x.retro_business[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_business_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_business.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_business.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_business_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_business.aware[[i]] <- w1
	x.retro_business.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_business <- as.data.frame(f.retro_business)
names(f.retro_business) <- c("fd", "lo", "up")
f.retro_business$year <- years
f.retro_business$dv <- "retro_business"
f.retro_business$type <- "Sociotropic"

f.retro_business.aware <- as.data.frame(rbind(f.retro_business.lo, f.retro_business.hi))
names(f.retro_business.aware) <- c("fd", "lo", "up")
f.retro_business.aware$year <- years
f.retro_business.aware$dv <- "retro_business"
f.retro_business.aware$type <- "Sociotropic"
f.retro_business.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))




########################################################################
#  DV: retro_war_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_war_f)))
years <- unique(md$year)

c.retro_war <- list()
x.retro_war <- list()
f.retro_war <- matrix(NA, ncol=3, nrow=length(years))

c.retro_war.aware <- list()
x.retro_war.aware <- list()
f.retro_war.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_war.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_war_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_war[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_war_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_war[[i]] <- w1
	x.retro_war[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_war_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_war.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_war.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_war_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_war.aware[[i]] <- w1
	x.retro_war.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_war <- as.data.frame(f.retro_war)
names(f.retro_war) <- c("fd", "lo", "up")
f.retro_war$year <- years
f.retro_war$dv <- "retro_war"
f.retro_war$type <- "Foreign"

f.retro_war.aware <- as.data.frame(rbind(f.retro_war.lo, f.retro_war.hi))
names(f.retro_war.aware) <- c("fd", "lo", "up")
f.retro_war.aware$year <- years
f.retro_war.aware$dv <- "retro_war"
f.retro_war.aware$type <- "Foreign"
f.retro_war.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))


########################################################################
#  DV: retro_forrel_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_forrel_f)))
years <- unique(md$year)

c.retro_forrel <- list()
x.retro_forrel <- list()
f.retro_forrel <- matrix(NA, ncol=3, nrow=length(years))

c.retro_forrel.aware <- list()
x.retro_forrel.aware <- list()
f.retro_forrel.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_forrel.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_forrel_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_forrel[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_forrel_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_forrel[[i]] <- w1
	x.retro_forrel[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_forrel_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_forrel.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_forrel.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_forrel_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_forrel.aware[[i]] <- w1
	x.retro_forrel.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_forrel <- as.data.frame(f.retro_forrel)
names(f.retro_forrel) <- c("fd", "lo", "up")
f.retro_forrel$year <- years
f.retro_forrel$dv <- "retro_forrel"
f.retro_forrel$type <- "Foreign"

f.retro_forrel.aware <- as.data.frame(rbind(f.retro_forrel.lo, f.retro_forrel.hi))
names(f.retro_forrel.aware) <- c("fd", "lo", "up")
f.retro_forrel.aware$year <- years
f.retro_forrel.aware$dv <- "retro_forrel"
f.retro_forrel.aware$type <- "Foreign"
f.retro_forrel.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))



########################################################################
#  DV: retro_world_f
########################################################################

md <- na.omit(subset(data, select=c(year, retro_world_f)))
years <- unique(md$year)

c.retro_world <- list()
x.retro_world <- list()
f.retro_world <- matrix(NA, ncol=3, nrow=length(years))

c.retro_world.aware <- list()
x.retro_world.aware <- list()
f.retro_world.lo <- matrix(NA, ncol=3, nrow=length(years))
f.retro_world.hi <- matrix(NA, ncol=3, nrow=length(years))



for(i in 1:length(years)){
z1 <- zelig(retro_world_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_world[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# STORE MODEL     
	w1 <- svyolr(retro_world_f~age+white+income+educ+female+Ind+OutParty, design=subset(wdata, year==years[i]))
	c.retro_world[[i]] <- w1
	x.retro_world[[i]] <- tidy.model.texreg(w1)


z1 <- zelig(retro_world_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, subset(data, year==years[i]), weight=subset(data, year==years[i])$weight, model="ologit")

	# SIMULATE FIRST DIFFERENCES: LOW AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=0)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=0)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_world.lo[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))

	# SIMULATE FIRST DIFFERENCES: HIGH AWARENESS  
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1, awareness=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0, awareness=1)
	z1$sim()
	t1 <- z1[["sim.out"]][["x1"]][["fd"]][[1]]
	f.retro_world.hi[i,] <- c(quantile(t1[,3], c(.5,.025,.975)))
	
	# STORE MODEL     
	w1 <- svyolr(retro_world_f~age+white+income+educ+female+Ind+OutParty+awareness*Ind+awareness*OutParty, design=subset(wdata, year==years[i]))
	c.retro_world.aware[[i]] <- w1
	x.retro_world.aware[[i]] <- tidy.model.texreg.aware(w1)
	
print(years[i])
}

f.retro_world <- as.data.frame(f.retro_world)
names(f.retro_world) <- c("fd", "lo", "up")
f.retro_world$year <- years
f.retro_world$dv <- "retro_world"
f.retro_world$type <- "Foreign"

f.retro_world.aware <- as.data.frame(rbind(f.retro_world.lo, f.retro_world.hi))
names(f.retro_world.aware) <- c("fd", "lo", "up")
f.retro_world.aware$year <- years
f.retro_world.aware$dv <- "retro_world"
f.retro_world.aware$type <- "Foreign"
f.retro_world.aware$aware <- c(rep("Lowest", length(years)), rep("Highest", length(years)))



########################################################################
#  Combine FDs into a dataframe
########################################################################

f.combined <- rbind(f.retro_business, f.retro_cost, f.retro_econ, f.retro_finstat, f.retro_income, f.retro_inflation, f.retro_unemp, f.retro_world, f.retro_war, f.retro_forrel, f.retro_general)
f.combined$j.year <- jitter(f.combined$year, factor=1.5)

f.combined.aware <- rbind(f.retro_business.aware, f.retro_cost.aware, f.retro_econ.aware, f.retro_finstat.aware, f.retro_income.aware, f.retro_inflation.aware, f.retro_unemp.aware, f.retro_world.aware, f.retro_war.aware, f.retro_forrel.aware, f.retro_general.aware)
f.combined.aware$j.year <- jitter(f.combined.aware$year, factor=1.5)



########################################################################
#  Table 1
########################################################################

texreg(x.retro_econ[c(1,15)], single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), custom.coef.names=c.names, symbol="\\dagger", fontsize="footnotesize", bold=-10000)



########################################################################
#  Table 2
########################################################################

z1 <- zelig(retro_econ_f~age+white+income+educ+female+Ind+OutParty, subset(data, year==1980), weight=subset(data, year==1980)$weight, model="ologit")
z2 <- zelig(retro_econ_f ~age+white+income+educ+female+Ind+OutParty, subset(data, year==2016), weight=subset(data, year==2016)$weight, model="ologit")
	
	# 1980   
	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=1, OutParty=0)
	z1$sim()
	summary(z1)
	round(quantile(z1[["sim.out"]][["x"]][["ev"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x"]][["ev"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x"]][["ev"]][[1]][,1], c(.5,.025,.975)),2)

	z1$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z1$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z1$sim()
	summary(z1)
	round(quantile(z1[["sim.out"]][["x"]][["ev"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x"]][["ev"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x"]][["ev"]][[1]][,1], c(.5,.025,.975)),2)

	round(quantile(z1[["sim.out"]][["x1"]][["ev"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x1"]][["ev"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x1"]][["ev"]][[1]][,1], c(.5,.025,.975)),2)
 	
	round(quantile(z1[["sim.out"]][["x1"]][["fd"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x1"]][["fd"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z1[["sim.out"]][["x1"]][["fd"]][[1]][,1], c(.5,.025,.975)),2)

	
	# 2016    
	z2$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=1, OutParty=0)
	z2$sim()
	summary(z2)
	round(quantile(z2[["sim.out"]][["x"]][["ev"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x"]][["ev"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x"]][["ev"]][[1]][,1], c(.5,.025,.975)),2)

	z2$setx(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=1)
	z2$setx1(age=4.52, white=1, income=3, educ=3, female=1, Ind=0, OutParty=0)
	z2$sim()
	summary(z2)
	round(quantile(z2[["sim.out"]][["x"]][["ev"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x"]][["ev"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x"]][["ev"]][[1]][,1], c(.5,.025,.975)),2)

	round(quantile(z2[["sim.out"]][["x1"]][["ev"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x1"]][["ev"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x1"]][["ev"]][[1]][,1], c(.5,.025,.975)),2)
 	
	round(quantile(z2[["sim.out"]][["x1"]][["fd"]][[1]][,3], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x1"]][["fd"]][[1]][,2], c(.5,.025,.975)),2)
	round(quantile(z2[["sim.out"]][["x1"]][["fd"]][[1]][,1], c(.5,.025,.975)),2)


########################################################################
#  Figure 1
########################################################################

ggplot(f.combined, aes(y=fd, x=j.year, ymin=lo, ymax=up)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=2, color="white", fill="black", alpha=.85, shape=21) +
	geom_smooth(aes(y=fd, x=year), data=f.combined, se=T, col="black") +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.2,.5), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.2,.5))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	ggtitle("(a) All evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig1_a.pdf", width=5.5, height=6)

ggplot(subset(f.combined, type=="Pocketbook"), aes(y=fd, x=j.year, ymin=lo, ymax=up)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=2, color="white", fill="black", alpha=.85, shape=21) +
	geom_smooth(aes(y=fd, x=year), data=subset(f.combined, type=="Pocketbook"), se=T, col="black") +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.2,.5), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.2,.5))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	ggtitle("(b) Pocketbook economic evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig1_b.pdf", width=5.5, height=6)		

ggplot(subset(f.combined, type=="Sociotropic"), aes(y=fd, x=j.year, ymin=lo, ymax=up)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=2, color="white", fill="black", alpha=.85, shape=21) +	
	geom_smooth(aes(y=fd, x=year), data=subset(f.combined, type=="Sociotropic"), se=T, col="black") +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.2,.5), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.2,.5))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	ggtitle("(c) Sociotropic economic evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,		
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig1_c.pdf", width=5.5, height=6)		

ggplot(subset(f.combined, type=="Foreign"), aes(y=fd, x=j.year, ymin=lo, ymax=up)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=2, color="white", fill="black", alpha=.85, shape=21) +
	geom_smooth(aes(y=fd, x=year), data=subset(f.combined, type=="Foreign"), se=T, col="black") +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.2,.5), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.2,.5))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	ggtitle("(d) Foreign affairs evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black"),
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig1_d.pdf", width=5.5, height=6)		
dev.off()	



# In-text analysis of Figure 1

loess.all <- predict(loess(fd~year, f.combined), 1956:2016, se=T)
loess.all <- cbind(1956:2016, loess.all$fit, loess.all$fit-(1.96* loess.all$se.fit), loess.all$fit+(1.96* loess.all$se.fit))
round(loess.all,2)

mean(subset(f.combined, type=="Sociotropic")[,1])
mean(subset(f.combined, type=="Pocketbook")[,1])
mean(subset(f.combined, type=="Foreign")[,1])

loess.for <- predict(loess(fd~year, subset(f.combined, type=="Foreign")), 1956:2016, se=T)
loess.for <- cbind(1956:2016, loess.for$fit, loess.for$fit-(1.96* loess.for$se.fit), loess.for$fit+(1.96* loess.for$se.fit))
round(loess.for,2)














########################################################################
#  Figure 2
########################################################################

ggplot(data=(f.combined.aware), aes(y=fd, x=j.year, ymin=lo, ymax=up, linetype=aware, shape=aware)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=1.5, color="white", fill="black", alpha=.85) +
	geom_smooth(se=T, col="black", size=1.35) +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.325,.612), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.3,.6))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	scale_linetype_manual(values=c(1,6), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	scale_shape_manual(values=c(21,23), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	ggtitle("(a) All evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig2_a.pdf", width=5.5, height=6)


ggplot(data=subset(f.combined.aware, type=="Pocketbook"), aes(y=fd, x=j.year, ymin=lo, ymax=up, linetype=aware, shape=aware)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=1.5, color="white", fill="black", alpha=.85) +
	geom_smooth(se=T, col="black", size=1.35) +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.325,.612), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.3,.6))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	scale_linetype_manual(values=c(1,6), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	scale_shape_manual(values=c(21,23), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	ggtitle("(b) Pocketbook economic evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig2_b.pdf", width=5.5, height=6)


ggplot(data=subset(f.combined.aware, type=="Sociotropic"), aes(y=fd, x=j.year, ymin=lo, ymax=up, linetype=aware, shape=aware)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=1.5, color="white", fill="black", alpha=.85) +
	geom_smooth(se=T, col="black", size=1.35) +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.325,.622), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.3,.6))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	scale_linetype_manual(values=c(1,6), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	scale_shape_manual(values=c(21,23), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	ggtitle("(c) Sociotropic economic evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig2_c.pdf", width=5.5, height=6)



ggplot(data=subset(f.combined.aware, type=="Foreign"), aes(y=fd, x=j.year, ymin=lo, ymax=up, linetype=aware, shape=aware)) +
	geom_hline(yintercept=0, color="white", size=1.1) +
	geom_errorbar(size=.2, width=.1, linetype=1, color="gray60") +
	geom_point(size=1.5, color="white", fill="black", alpha=.85) +
	geom_smooth(se=T, col="black", size=1.35) +
	scale_x_continuous(limits=c(1950,2020), breaks=c(1950,1960,1970,1980,1990,2000,2010,2020)) +
	scale_y_continuous(limits=c(-.325,.612), breaks=c(-.3,-.2,-.1,0,.1,.2,.3,.4,.5,.6)) +
	coord_cartesian(ylim=c(-.3,.6))+
	xlab("") + ylab("Difference in probability of in- and out-partisans saying conditions got better") +
	scale_linetype_manual(values=c(1,6), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	scale_shape_manual(values=c(21,23), breaks=c("Lowest", "Highest"), name="Political awareness", guide=FALSE) +
	ggtitle("(d) Foreign affairs evaluations")	+
		theme(panel.background=element_rect(fill="gray91"), 
		axis.text=element_text(size=10, color="black") ,
		axis.text.y=element_text(size=8, color="black") ,
		plot.title=element_text(hjust=.5, face="bold"))
ggsave("Fig2_d.pdf", width=5.5, height=6)




fake.dat <- subset(f.combined.aware, type=="Foreign")
fake.dat$aware <- recode(fake.dat$aware, "'Lowest'='Lowest     '")

g1 <- ggplot(data=fake.dat, aes(y=fd, x=j.year, ymin=lo, ymax=up, linetype=aware, shape=aware)) +
	geom_point(size=2.75, color="white", fill="black", alpha=.85) +
	geom_smooth(se=F, col="black", size=.9) +
	scale_linetype_manual(values=c(1,6), breaks=c("Lowest     ", "Highest"), name="Political awareness") +
	scale_shape_manual(values=c(21,23), breaks=c("Lowest     ", "Highest"), name="Political awareness") +
		theme(panel.background=element_rect(fill="gray91"))
gd <- g1 + theme(legend.position="top", legend.direction="horizontal", legend.key.width=unit(1.2, "cm"), legend.text=element_text(size=10), legend.title=element_text(size=11)) + guides(linetype=guide_legend(title.position="top", title.hjust=.5))		
gd		
ggsave("Fig2_legend.pdf", width=5.5, height=6)
dev.off()








f.combined.aware$is.sig <- ifelse(f.combined.aware$lo>0,1,0)
mean(subset(f.combined.aware, aware=="Lowest")$is.sig)
mean(subset(f.combined.aware, type=="Pocketbook"&aware=="Lowest")$is.sig)
mean(subset(f.combined.aware, type=="Sociotropic"&aware=="Lowest")$is.sig)
mean(subset(f.combined.aware, type=="Foreign"&aware=="Lowest")$is.sig)

mean(subset(f.combined.aware, aware=="Highest")$is.sig)
mean(subset(f.combined.aware, type=="Pocketbook"&aware=="Highest")$is.sig)
mean(subset(f.combined.aware, type=="Sociotropic"&aware=="Highest")$is.sig)
mean(subset(f.combined.aware, type=="Foreign"&aware=="Highest")$is.sig)


loess.lo <- predict(loess(fd~year, subset(f.combined.aware, aware=="Lowest")), 1956:2016, se=T)
loess.lo <- cbind(1956:2016, loess.lo$fit, loess.lo$fit-(1.96* loess.lo$se.fit), loess.lo$fit+(1.96* loess.lo$se.fit))
round(loess.lo,2)

loess.hi <- predict(loess(fd~year, subset(f.combined.aware, aware=="Highest")), 1956:2016, se=T)
loess.hi <- cbind(1956:2016, loess.hi$fit, loess.hi$fit-(1.96* loess.hi$se.fit), loess.hi$fit+(1.96* loess.hi$se.fit))
round(loess.hi,2)



loess.lo.pocket <- predict(loess(fd~year, subset(f.combined.aware, type=="Pocketbook"&aware=="Lowest")), 1956:2016, se=T)
loess.lo.pocket <- cbind(1956:2016, loess.lo.pocket$fit, loess.lo.pocket$fit-(1.96* loess.lo.pocket$se.fit), loess.lo.pocket$fit+(1.96* loess.lo.pocket$se.fit))
round(loess.lo.pocket,2)

loess.hi.pocket <- predict(loess(fd~year, subset(f.combined.aware, type=="Pocketbook"&aware=="Highest")), 1956:2016, se=T)
loess.hi.pocket <- cbind(1956:2016, loess.hi.pocket $fit, loess.hi.pocket $fit-(1.96* loess.hi.pocket $se.fit), loess.hi.pocket $fit+(1.96* loess.hi.pocket $se.fit))
round(loess.hi.pocket,2)



save.image("pejones_PB_indiv_analysis.RData")
